import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue')
    },
    {
        path: '/',
        redirect: { name: 'Login' }
    },

    {
        path: '/home',
        name: 'Home',
        redirect: '/about',
        component: () =>
            import ('../views/Home.vue'),
        children: [{
                path: '/about',
                component: () =>
                    import ('@/views/About.vue')
            },
            {
                path: '/users',
                name: 'User',
                component: () =>
                    import ('@/views/aside/User.vue')

            },
            {
                path: '/rights',
                name: 'Rights',
                component: () =>
                    import ('@/views/aside/Rights.vue')

            },
            {
                path: '/roles',
                name: 'Roles',
                component: () =>
                    import ('@/views/aside/Roles.vue')

            },
            {
                path: '/categories',
                name: 'Categoods',
                component: () =>
                    import ('@/views/aside/Categoods.vue')

            },
            {
                path: '/table',

                component: () =>
                    import ('@/views/table.vue')

            },
            {
                path: '/params',

                component: () =>
                    import ('@/views/aside/Params.vue')

            },
            {
                path: '/goods',

                component: () =>
                    import ('@/views/aside/Goods.vue'),


            },
            {
                path: '/goods/add',
                component: () =>
                    import ('@/views/aside/Add.vue')
            },
            {
                path: '/orders',
                component: () =>
                    import ('@/views/aside/Orders.vue')
            },
            {
                path: '/reports',
                component: () =>
                    import ('@/views/aside/Reports.vue')
            },
        ]

    },
    {
        path: '/home',
        redirect: '/home/about'
    }


]



const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next()
    } else {
        let token = window.sessionStorage.getItem('token')
        if (token) {
            next()
        } else {
            next('/login')
        }
    }
})


export default router